<template>
  <div>
    <!--搜索框-->
    <div class="pd-10">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称" class="mr-5" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <!--新增按钮-->
    <div class="mg-10">
      <!--导入-->
      <el-upload :action="action" style="display: inline-block"
                 :show-file-list="false" :on-success="handleFileUploadSuccess">
        <el-button class="ml-5" type="primary"><i class="el-icon-bottom"></i>上传文件</el-button>
      </el-upload>
      <!--批量删除-->
      <el-popconfirm
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch">
        <el-button class="ml-5" type="danger" slot="reference">
          <i class="el-icon-remove-outline"></i>批量删除
        </el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData" border stripe :header-cell-style="{background: '#cccccc'}"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column prop="id" label="ID" width="50">
      </el-table-column>
      <el-table-column prop="name" label="文件名称" width="140">
      </el-table-column>
      <el-table-column prop="type" label="文件类型" width="120">
      </el-table-column>
      <el-table-column prop="size" label="文件大小(KB)">
      </el-table-column>
      <el-table-column label="下载">
        <template slot-scope="scope">
          <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="启用">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#cccccc" @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)">
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {serverIp, serverPort} from "../../public/config";

export default {
  name: "File",
  data() {
    return {
      tableData: [],
      name: "",
      multipleSelection: [],
      //分页
      pageNum: 1,
      pageSize: 5,
      total: 0,
      action: `http://${serverIp}:${serverPort}/file/upload`
    }
  },
  methods: {
    load() {
      this.request.get("/file/page",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    del(id) {
      this.request.delete("/file/"+id).then(res => {
        if (res.code === "200") {
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      this.request.post("/file/del/batch",ids).then(res => {
        if (res.code === "200") {
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.load()
    },
    handleFileUploadSuccess(res) {
      console.log(res)
      this.load()
    },
    download(url) {
      window.open(url)
    },
    changeEnable(row) {
      // console.log(row)
      this.request.post("/file/update",row).then(res => {
        if (res.code === "200") {
          this.$message.success("操作成功")
          this.load()
        }
      })
    }
  },
  created() {
    this.load()
  }
}
</script>

<style scoped>

</style>